#coding:utf8
"""

"""
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB, MultinomialNB
from sklearn.metrics import classification_report, confusion_matrix, roc_curve, auc
import matplotlib.pyplot as plt
import joblib


def plot_roc(y_true, y_pred):
    fpr, tpr, threshold = roc_curve(y_true, y_pred[:,1])
    roc_auc = auc(fpr, tpr)
    plt.figure(figsize=(6,6))
    plt.title('Validation ROC')
    plt.plot(fpr, tpr, 'b', label = 'Val AUC = %0.3f' % roc_auc)
    plt.legend(loc = 'lower right')
    plt.plot([0, 1], [0, 1],'r--')
    plt.xlim([0, 1])
    plt.ylim([0, 1])
    plt.ylabel('True Positive Rate')
    plt.xlabel('False Positive Rate')
    plt.show()


def load_data(filePath):
    Y, X = [], []
    with open(filePath, encoding='utf8') as f:
        line = f.readline()
        while line.strip():
            t1 = line.strip().split("\t")
            #t2 = [float(m) for m in t1[1].split()]
            t2 = t1[1].split()
            Y.append(int(t1[0]))
            X.append(t1[1])

            line = f.readline()

    return Y, X


def run(filePath):
    #filePath = "train_data_0825_01.txt"
    print(f"loadingg data ...")
    Y,X = load_data(filePath)
    x_train_tmp,x_test_tmp,y_train,y_test = train_test_split(X,Y,stratify=Y,test_size=0.2,random_state=200)

    x_train, x_train_info = [], []
    for m in x_train_tmp:
        t1 = m.split()
        x_train.append(t1)
        #x_train.append(t1[:-2])
        #x_train_info.append((t1[-1], t1[-2]))

    x_test, x_test_info = [], []
    for m in x_test_tmp:
        t1 = m.split()
        x_test.append(t1)
        #x_test.append(t1[:-2])
        #x_test_info.append((t1[-1], t1[-2]))

    # for m in [500,800,1000]:
    # #for m in [{0:0.5,1:1},{0:1,1:3},{0:1,1:5},{0:1,1:8}]:
    #     print(m)
    #     clf = RandomForestClassifier(n_estimators=m, class_weight={1:1,1:8})
    #     print("training ...")
    #     clf.fit(x_train, y_train)
    #     pred_train = clf.predict(x_train)
    #     pred_test = clf.predict(x_test)
    #     pred_prob = clf.predict_proba(x_test)
    #     # pred = pred_prob[:,1].tolist()
    #     print(clf.classes_)
    #     print(classification_report(y_train, pred_train))
    #     print(confusion_matrix(y_train, pred_train))
    #     print()
    #     print(classification_report(y_test, pred_test))
    #     print(confusion_matrix(y_test, pred_test))

    clf = RandomForestClassifier(n_estimators=500, n_jobs=4, class_weight={0:1,1:8}, min_samples_split=20, min_samples_leaf=10, max_samples=0.8)
    print("training ...")
    clf.fit(x_train, y_train)
    pred_train = clf.predict(x_train)
    pred_test = clf.predict(x_test)
    pred_prob = clf.predict_proba(x_test)
    print(clf.classes_)
    print(clf.n_features_)
    print(classification_report(y_train, pred_train))
    print(confusion_matrix(y_train, pred_train))
    print()
    print(classification_report(y_test, pred_test))
    print(confusion_matrix(y_test, pred_test))
    plot_roc(y_test, pred_prob)
    #
    # #save
    modelPath = "../resources/20221022_02_rf.m"
    joblib.dump(clf, modelPath)


if __name__=="__main__":
    filePath = "../resources/train_data_1022_02.txt"
    run(filePath)
